<?php
/**
 * APC文件上传客户端
 *  示例1：upload.php
 * <pre>
 * 	 <form action="/upload/post" id="new_video" target="upload_iframe" name="new_video" method="post" enctype="multipart/form-data">
 *	 	<input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="<?=uniqid();?>"/>
 *	 	<input type="file" id="1ile1" name="file1"/>
 *	 	<input type="submit" value="提交"  onclick="return upx();" />
 *	 </form>
 * 	 <iframe  src="" name="upload_iframe" width="600" height="400"></iframe>
 * </pre>
  *  示例2:post.php 开始上传
 * <pre>
 * 			$up = new IApcUpLoadClient("file1");
 *			$up->setP("/home/peter/code/Frog/web/tongshihui/tmp/");
 * 			$up->up(); 
 * </pre>
  * 	示例3:get.php	获取上传中的信息
 * <pre>
 * 			$up->s();
 * </pre>
 * @name IApcUpLoadClient 
 * @version $Id: MemcacheClient.php 312 2008-05-14 23:37:21 peter $
 * @package net.frog.auc
 * @author peter <openjsp@gmail.com>
 * @since 0.2 rc6
 */
class IApcUpLoadClient extends IApcUpLoadHelper {
			
			private $typeArray;
			
			/**
			 * 上传文件
			 *	  6 文件类型不正确
			 *   7 超出上传文件大小
			 * @return boolean
			 */
			public function up(){
					if($_SERVER["REQUEST_METHOD"] == "POST"){
							if($this->getError() != 0){
						  		return $this->getError();
						  	}
							$type = $this->mimeContentType();
						  if($this->isType($type["type"]) == false){
						  		return 6;
						  	}
						  if($this->isMaxSize() == false){
						  		return 7;
						  	}
						  if(is_uploaded_file($this->getTmpName())){
						  		move_uploaded_file($this->getTmpName(),$this->getP(). $this->getNewFileName());
						  	}
						  return true;
					}
					return false;
			}
			
			/**
			 * 判断是否为合法上传的文件类型
			 *
			 * @param String $type
			 * @return boolean
			 */
			public function isType($type){
					$this->typeArray = explode("|",$this->getRestrictionsFileType());
					if(in_array($type,$this->typeArray)){
							return true;
					}
					return false;
			}
			/**
			 * 判断文件是否超出规定大小
			 *
			 * @return boolean
			 */
			public function isMaxSize(){
					if($this->getSize() <= $this->getMaxFileSize()){
							return true;
					}
					return false;
			}
			/**
			 * 获取上传进度
			 * @name getStatus  => s 
			 * @param String $key
			 * @return array
			 *   <pre>
			 * 			array(
			 * 					"total"=>1234,	文件大小
			 * 					"scale"=>21.21,	上传比例
			 * 					"done"=>1				是否上传成功
			  * 			)	
			 * 	 </pre>
			 */
			public function s($key = "progress_key"){
					$status = $this->getApcFetch($key);
					$upScale = 0;
					if ( $status['current'] > 0 ) {
							$upScale  = ($status['current']/$status['total'])*100;
					}
					$upFetch = array(
							"total"=>$status['total'],
							"scale"=>round($upScale,2),
							"done"=>$status['done']
					);
					return json_encode($upFetch);
			}
			/**
			 * 获取apc上传信息
			 *
			 * @param String $key
			 * @return array
			 * total					 文件的总大小 
			 * current 				 到目前为止收到的文件数 
			 * rate   				 上传速度（以字节每秒为单位） 
			 * filename  			 文件名 
			 * name   				 变量名
			 * temp_filename   PHP 保存文件的临时副本的位置 
			 * cancel_upload   上传是已取消 (1)，还是未取消 (0) 
			 * done   				 上传是已完成 (1)，还是尚未完成 (0) 
			 */
			public function getApcFetch($key = "progress_key"){
						return apc_fetch("upload_".$_GET[$key]);
			}
}

?>
